// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

import org.apache.doris.regression.action.ExplainAction

suite("test_datetimev2_precision_promotion") {
    sql "SET enable_nereids_planner=true"
    sql "SET enable_fallback_to_original_planner=false"
    sql "drop table if exists dt36d"
    sql """
        create table dt36d(
            k0 datetime(3) null,
            k1 datetime(6) null,
            k2 date null
        )
        DISTRIBUTED BY HASH(`k0`) BUCKETS auto
        properties("replication_num" = "1");
    """
    sql "insert into dt36d values ('2020-12-12', '2020-12-12', '2020-12-12');"

    explain {
        sql "select nvl(k0, k2) as r1 from dt36d"
        checkSlotTypeOf ("r1", "datetimev2(3)") // only promote to 3
        checkSlotTypeOf ("k0", "datetimev2(3)")
        checkSlotTypeOf ("k2", "datev2")
    }
    explain {
        sql "select nvl(k1, k2) as r1 from dt36d"
        checkSlotTypeOf ("r1", "datetimev2(6)")
        checkSlotTypeOf ("k1", "datetimev2(6)")
        checkSlotTypeOf ("k2", "datev2")
    }
    explain {
        sql "select nvl(k0, k1) as r1 from dt36d"
        checkSlotTypeOf ("r1", "datetimev2(6)")
        checkSlotTypeOf ("k0", "datetimev2(3)")
        checkSlotTypeOf ("k1", "datetimev2(6)")
    }
}
